package demo.db.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor.Builder;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HBaseAdminDemo {

    public static Configuration configuration = HBaseConfiguration.create();

    static {
        // zookeeper地址
        configuration.set("hbase.zookeeper.quorum", "172.17.16.61:2181,172.17.16.219:2181,172.17.16.220:2181");
        // configuration.set("hbase.zookeeper.property.clientPort", "2181");//
        // zookeeper端口
        configuration.set("zookeeper.znode.parent", "/hbase");
    }

    /**
     * 创建表命名空间
     * 
     * @param namespace
     * @throws IOException
     */
    public void createNamespace(String namespace) throws IOException {
        HBaseAdmin admina;

        try (Connection connection = ConnectionFactory.createConnection(configuration);
                Admin admin = connection.getAdmin();) {
            Builder builder = NamespaceDescriptor.create(namespace);
            NamespaceDescriptor namespaceDescriptor = builder.build();
            admin.createNamespace(namespaceDescriptor);
        }
    }
}
